package com.fr.report;

import com.fr.base.BaseUtils;
import com.fr.base.FCloneable;
import com.fr.base.StringUtils;
import com.fr.base.Style;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.serializable.ImageSerializable;
import com.fr.base.xml.XMLObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.data.core.DataXMLUtils;
import com.fr.report.cellElement.BarcodeAttr;
import com.fr.report.cellElement.BarcodePresent;
import com.fr.report.cellElement.CellExpandAttr;
import com.fr.report.cellElement.CellGUIAttr;
import com.fr.report.cellElement.CellInsertPolicyAttr;
import com.fr.report.cellElement.CellPageAttr;
import com.fr.report.cellElement.DictPresent;
import com.fr.report.cellElement.Present;
import com.fr.report.cellElement.core.CellElementAttribute;
import com.fr.report.cellElement.core.DSColumn;
import com.fr.report.cellElement.core.EM;
import com.fr.report.highlight.HighlightGroup;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.io.xml.SynchronizedNameWidget;
import com.fr.report.io.xml.SynchronizedStyleList;
import com.fr.report.io.xml.SynchronizedVersion;
import com.fr.report.js.NameJavaScriptGroup;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Primitive;
import com.fr.report.web.ui.Widget;
import java.awt.Image;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class AbstractCellElement
  implements CellElement
{
  protected Style style = Style.DEFAULT_STYLE;
  protected EM optionalAttributes = EM.EMPTY_ATTR;
  protected Map attributes = null;
  protected transient Object value = Primitive.NULL;

  public Object getValue()
  {
    return this.value;
  }

  public void setValue(Object paramObject)
  {
    this.value = paramObject;
  }

  public Iterator attributeKeyIterator()
  {
    if (this.attributes == null)
      return Collections.EMPTY_LIST.iterator();
    return this.attributes.keySet().iterator();
  }

  public void setAttribute(Object paramObject1, Object paramObject2)
  {
    if (paramObject1 == null)
      return;
    if (paramObject2 == null)
    {
      removeAttribute(paramObject1);
      return;
    }
    if (this.attributes == null)
      this.attributes = new HashMap();
    this.attributes.put(paramObject1, paramObject2);
  }

  public Object getAttribute(Object paramObject)
  {
    return ((this.attributes == null) ? null : this.attributes.get(paramObject));
  }

  public Object removeAttribute(Object paramObject)
  {
    Object localObject = null;
    if ((this.attributes != null) && (paramObject != null))
    {
      localObject = this.attributes.remove(paramObject);
      if (this.attributes.size() == 0)
        this.attributes = null;
    }
    return localObject;
  }

  public Style getStyle()
  {
    if (this.style == null)
      this.style = Style.DEFAULT_STYLE;
    return this.style;
  }

  public void setStyle(Style paramStyle)
  {
    this.style = paramStyle;
  }

  public NameJavaScriptGroup getNameHyperlinkGroup()
  {
    return ((NameJavaScriptGroup)this.optionalAttributes.get(CellElementAttribute.NAMEHYPERLINKGROUP));
  }

  public void setNameHyperlinkGroup(NameJavaScriptGroup paramNameJavaScriptGroup)
  {
    if (paramNameJavaScriptGroup == null)
    {
      this.optionalAttributes.remove(CellElementAttribute.NAMEHYPERLINKGROUP);
      if (this.optionalAttributes.size() == 0)
        this.optionalAttributes = EM.EMPTY_ATTR;
      return;
    }
    if (this.optionalAttributes == EM.EMPTY_ATTR)
      this.optionalAttributes = new EM();
    this.optionalAttributes.put(CellElementAttribute.NAMEHYPERLINKGROUP, paramNameJavaScriptGroup);
  }

  public HighlightGroup getHighlightGroup()
  {
    return ((HighlightGroup)this.optionalAttributes.get(CellElementAttribute.HIGHLIGHTGROUP));
  }

  public void setHighlightGroup(HighlightGroup paramHighlightGroup)
  {
    if (paramHighlightGroup == null)
    {
      this.optionalAttributes.remove(CellElementAttribute.HIGHLIGHTGROUP);
      if (this.optionalAttributes.size() == 0)
        this.optionalAttributes = EM.EMPTY_ATTR;
      return;
    }
    if (this.optionalAttributes == EM.EMPTY_ATTR)
      this.optionalAttributes = new EM();
    this.optionalAttributes.put(CellElementAttribute.HIGHLIGHTGROUP, paramHighlightGroup);
  }

  public Present getPresent()
  {
    return ((Present)this.optionalAttributes.get(CellElementAttribute.PRESENT));
  }

  public void setPresent(Present paramPresent)
  {
    if (paramPresent == null)
    {
      this.optionalAttributes.remove(CellElementAttribute.PRESENT);
      if (this.optionalAttributes.size() == 0)
        this.optionalAttributes = EM.EMPTY_ATTR;
      return;
    }
    if (this.optionalAttributes == EM.EMPTY_ATTR)
      this.optionalAttributes = new EM();
    this.optionalAttributes.put(CellElementAttribute.PRESENT, paramPresent);
  }

  public CellGUIAttr getCellGUIAttr()
  {
    return ((CellGUIAttr)this.optionalAttributes.get(CellElementAttribute.CELLGUIATTR));
  }

  public void setCellGUIAttr(CellGUIAttr paramCellGUIAttr)
  {
    if (paramCellGUIAttr == null)
    {
      this.optionalAttributes.remove(CellElementAttribute.CELLGUIATTR);
      if (this.optionalAttributes.size() == 0)
        this.optionalAttributes = EM.EMPTY_ATTR;
      return;
    }
    if (this.optionalAttributes == EM.EMPTY_ATTR)
      this.optionalAttributes = new EM();
    this.optionalAttributes.put(CellElementAttribute.CELLGUIATTR, paramCellGUIAttr);
  }

  public CellExpandAttr getCellExpandAttr()
  {
    return ((CellExpandAttr)this.optionalAttributes.get(CellElementAttribute.CELLEXPANDATTR));
  }

  public void setCellExpandAttr(CellExpandAttr paramCellExpandAttr)
  {
    if (paramCellExpandAttr == null)
    {
      this.optionalAttributes.remove(CellElementAttribute.CELLEXPANDATTR);
      if (this.optionalAttributes.size() == 0)
        this.optionalAttributes = EM.EMPTY_ATTR;
      return;
    }
    if (this.optionalAttributes == EM.EMPTY_ATTR)
      this.optionalAttributes = new EM();
    this.optionalAttributes.put(CellElementAttribute.CELLEXPANDATTR, paramCellExpandAttr);
  }

  public CellInsertPolicyAttr getCellInsertPolicyAttr()
  {
    return ((CellInsertPolicyAttr)this.optionalAttributes.get(CellElementAttribute.CELLINSERTPOLICYATTR));
  }

  public void setCellInsertPolicyAttr(CellInsertPolicyAttr paramCellInsertPolicyAttr)
  {
    if (paramCellInsertPolicyAttr == null)
    {
      this.optionalAttributes.remove(CellElementAttribute.CELLINSERTPOLICYATTR);
      if (this.optionalAttributes.size() == 0)
        this.optionalAttributes = EM.EMPTY_ATTR;
      return;
    }
    if (this.optionalAttributes == EM.EMPTY_ATTR)
      this.optionalAttributes = new EM();
    this.optionalAttributes.put(CellElementAttribute.CELLINSERTPOLICYATTR, paramCellInsertPolicyAttr);
  }

  public Widget getWidget()
  {
    return ((Widget)this.optionalAttributes.get(CellElementAttribute.WIDGET));
  }

  public void setWidget(Widget paramWidget)
  {
    if (paramWidget == null)
    {
      this.optionalAttributes.remove(CellElementAttribute.WIDGET);
      if (this.optionalAttributes.size() == 0)
        this.optionalAttributes = EM.EMPTY_ATTR;
      return;
    }
    if (this.optionalAttributes == EM.EMPTY_ATTR)
      this.optionalAttributes = new EM();
    this.optionalAttributes.put(CellElementAttribute.WIDGET, paramWidget);
  }

  public CellPageAttr getCellPageAttr()
  {
    return ((CellPageAttr)this.optionalAttributes.get(CellElementAttribute.PAGE));
  }

  public void setCellPageAttr(CellPageAttr paramCellPageAttr)
  {
    if (paramCellPageAttr == null)
    {
      this.optionalAttributes.remove(CellElementAttribute.PAGE);
      if (this.optionalAttributes.size() == 0)
        this.optionalAttributes = EM.EMPTY_ATTR;
      return;
    }
    if (this.optionalAttributes == EM.EMPTY_ATTR)
      this.optionalAttributes = new EM();
    this.optionalAttributes.put(CellElementAttribute.PAGE, paramCellPageAttr);
  }

  public String getScript()
  {
    return ((String)this.optionalAttributes.get(CellElementAttribute.SCRIPT));
  }

  public void setScript(String paramString)
  {
    if (paramString == null)
    {
      this.optionalAttributes.remove(CellElementAttribute.SCRIPT);
      if (this.optionalAttributes.size() == 0)
        this.optionalAttributes = EM.EMPTY_ATTR;
      return;
    }
    if (this.optionalAttributes == EM.EMPTY_ATTR)
      this.optionalAttributes = new EM();
    this.optionalAttributes.put(CellElementAttribute.SCRIPT, paramString);
  }

  public EM getOptionalAttributes()
  {
    return this.optionalAttributes;
  }

  public void setOptionalAttributes(EM paramEM)
  {
    this.optionalAttributes = paramEM;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    String str1;
    if (paramXMLableReader.isAttr())
    {
      if ((str1 = paramXMLableReader.getAttr("c")) != null)
        setColumn(Math.max(Integer.parseInt(str1), 0));
      if ((str1 = paramXMLableReader.getAttr("r")) != null)
        setRow(Math.max(Integer.parseInt(str1), 0));
      if ((str1 = paramXMLableReader.getAttr("cs")) != null)
        setColumnSpan(Math.max(Integer.parseInt(str1), 1));
      if ((str1 = paramXMLableReader.getAttr("rs")) != null)
        setRowSpan(Math.max(Integer.parseInt(str1), 1));
      if ((str1 = paramXMLableReader.getAttr("s")) != null)
        SynchronizedStyleList.getSynchronizedStyleList(Thread.currentThread()).put(Integer.parseInt(str1), this);
    }
    if (paramXMLableReader.isChildNode())
    {
      Object localObject;
      String str2 = paramXMLableReader.getTagName();
      if (("Object".equals(str2)) || (str2.equals("O")))
      {
        localObject = ReportXMLUtils.readObject(paramXMLableReader, true, this);
        if (localObject instanceof Parameter)
          localObject = SynchronizedNameWidget.get(((Parameter)localObject).getName());
        setValue(localObject);
      }
      else if (("Style".equals(str2)) || (str2.equals("CellStyle")))
      {
        if ((str1 = paramXMLableReader.getAttr("index")) != null)
          SynchronizedStyleList.getSynchronizedStyleList(Thread.currentThread()).put(Integer.parseInt(str1), this);
      }
      else if ("Present".equals(str2))
      {
        setPresent(ReportXMLUtils.readPresent(paramXMLableReader));
      }
      else if (("NameJavaScriptGroup".equals(str2)) || ("NameHyperlinkGroup".equals(str2)) || ("NameHyperlinks".equals(str2)) || ("HyperlinkMap".equals(str2)))
      {
        localObject = new NameJavaScriptGroup();
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        setNameHyperlinkGroup((NameJavaScriptGroup)localObject);
      }
      else if ("HighlightList".equals(str2))
      {
        localObject = new HighlightGroup();
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        setHighlightGroup((HighlightGroup)localObject);
      }
      else if ("Expand".equals(str2))
      {
        localObject = new CellExpandAttr();
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        setCellExpandAttr((CellExpandAttr)localObject);
        if (!(getValue() instanceof DSColumn))
        {
          SynchronizedVersion localSynchronizedVersion = SynchronizedVersion.getSynchronizedVersion(Thread.currentThread());
          if (localSynchronizedVersion.getVersion() == 6.1D)
            ((CellExpandAttr)localObject).setDirection(2);
        }
      }
      else if ("CellGUIAttr".equals(str2))
      {
        localObject = new XMLObject(this, new CellGUIAttr())
        {
          private final AbstractCellElement this$0;

          public void readXML()
          {
            if (paramXMLableReader.isAttr())
            {
              ((CellGUIAttr)this.obj).readXML(paramXMLableReader);
            }
            else if (paramXMLableReader.isChildNode())
            {
              String str = paramXMLableReader.getTagName();
              if (("Widget".equals(str)) || ("CellEditorDef".equals(str)))
                this.this$0.setWidget(ReportXMLUtils.readCellWidget(paramXMLableReader));
              else
                ((CellGUIAttr)this.obj).readXML(paramXMLableReader);
            }
          }
        };
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        setCellGUIAttr((CellGUIAttr)((XMLObject)localObject).getObject());
      }
      else if (CellInsertPolicyAttr.XML_TAG.equals(str2))
      {
        localObject = new CellInsertPolicyAttr();
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        setCellInsertPolicyAttr((CellInsertPolicyAttr)localObject);
      }
      else if (("Widget".equals(str2)) || ("CellEditorDef".equals(str2)))
      {
        setWidget(ReportXMLUtils.readWidget(paramXMLableReader));
      }
      else if (("CellPageAttr".equals(str2)) || ("CellAttr".equals(str2)))
      {
        localObject = new CellPageAttr();
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        setCellPageAttr((CellPageAttr)localObject);
      }
      else if ("Script".equals(str2))
      {
        if ((str1 = paramXMLableReader.getElementValue()) != null)
          setScript(str1);
      }
      else if ("CellExpandAttr".equals(str2))
      {
        localObject = new CellExpandAttr();
        paramXMLableReader.readXMLObject(new XMLReadable(this, (CellExpandAttr)localObject)
        {
          private final CellExpandAttr val$attr;
          private final AbstractCellElement this$0;

          public void readXML()
          {
            if (paramXMLableReader.isAttr())
            {
              String str;
              if ((str = paramXMLableReader.getAttr("direction")) != null)
                this.val$attr.setDirection((byte)Integer.parseInt(str));
              else
                this.val$attr.setDirection(0);
              if ((str = paramXMLableReader.getAttr("parentDefault")) != null)
              {
                this.val$attr.setLeftParentDefault(str.equalsIgnoreCase("true"));
                this.val$attr.setUpParentDefault(str.equalsIgnoreCase("true"));
              }
              if ((str = paramXMLableReader.getAttr("leftParentDefault")) != null)
                this.val$attr.setLeftParentDefault(str.equalsIgnoreCase("true"));
              if ((str = paramXMLableReader.getAttr("left")) != null)
              {
                this.val$attr.setLeftParentColumnRow(BaseUtils.convertCellStringToColumnRow(str));
                this.val$attr.setLeftParentDefault(false);
              }
              if ((str = paramXMLableReader.getAttr("upParentDefault")) != null)
                this.val$attr.setUpParentDefault(str.equalsIgnoreCase("true"));
              if ((str = paramXMLableReader.getAttr("up")) != null)
              {
                this.val$attr.setUpParentColumnRow(BaseUtils.convertCellStringToColumnRow(str));
                this.val$attr.setUpParentDefault(false);
              }
            }
          }
        });
        setCellExpandAttr((CellExpandAttr)localObject);
      }
      else if ("BarcodeAttr".equals(str2))
      {
        localObject = new BarcodeAttr();
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        setPresent(new BarcodePresent((BarcodeAttr)localObject));
      }
      else if (("Dict".equals(str2)) || ("Map".equals(str2)) || ("MapFactory".equals(str2)))
      {
        setPresent(new DictPresent(DataXMLUtils.readXMLDictionary(paramXMLableReader)));
      }
      else if ("Hyperlink".equals(str2))
      {
        setNameHyperlinkGroup(new NameJavaScriptGroup(ReportXMLUtils.readJavaScript(paramXMLableReader)));
      }
      else if ("Location".equals(str2))
      {
        if ((str1 = paramXMLableReader.getAttr("column")) != null)
          setColumn(Integer.parseInt(str1));
        else if ((str1 = paramXMLableReader.getAttr("c")) != null)
          setColumn(Integer.parseInt(str1));
        if ((str1 = paramXMLableReader.getAttr("row")) != null)
          setRow(Integer.parseInt(str1));
        else if ((str1 = paramXMLableReader.getAttr("r")) != null)
          setRow(Integer.parseInt(str1));
        if ((str1 = paramXMLableReader.getAttr("columnSpan")) != null)
          setColumnSpan(Integer.parseInt(str1));
        else if ((str1 = paramXMLableReader.getAttr("columnCount")) != null)
          setColumnSpan(Integer.parseInt(str1));
        if ((str1 = paramXMLableReader.getAttr("rowSpan")) != null)
          setRowSpan(Integer.parseInt(str1));
        else if ((str1 = paramXMLableReader.getAttr("rowCount")) != null)
          setRowSpan(Integer.parseInt(str1));
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    Object localObject;
    if ((localObject = getValue()) != null)
      ReportXMLUtils.writeObject(paramXMLPrintWriter, localObject);
    if ((localObject = getNameHyperlinkGroup()) != null)
      ((NameJavaScriptGroup)localObject).writeXML(paramXMLPrintWriter);
    if (((localObject = getHighlightGroup()) != null) && (((HighlightGroup)localObject).size() > 0))
      ((HighlightGroup)localObject).writeXML(paramXMLPrintWriter);
    if ((localObject = getPresent()) != null)
      ReportXMLUtils.writeXMLable(paramXMLPrintWriter, (Present)localObject, "Present");
    if ((localObject = getCellGUIAttr()) != null)
      ((CellGUIAttr)localObject).writeXML(paramXMLPrintWriter);
    if ((localObject = getCellExpandAttr()) != null)
      ((CellExpandAttr)localObject).writeXML(paramXMLPrintWriter);
    if ((localObject = getCellInsertPolicyAttr()) != null)
      ((CellInsertPolicyAttr)localObject).writeXML(paramXMLPrintWriter);
    if ((localObject = getWidget()) != null)
      ReportXMLUtils.writeXMLable(paramXMLPrintWriter, (Widget)localObject, "Widget");
    if ((localObject = getCellPageAttr()) != null)
      ((CellPageAttr)localObject).writeXML(paramXMLPrintWriter);
    String str = getScript();
    if (StringUtils.isNotBlank(str))
      paramXMLPrintWriter.startTAG("Script").textNode(str).end();
  }

  public CellElement deriveCellElement(int paramInt1, int paramInt2)
  {
    AbstractCellElement localAbstractCellElement;
    try
    {
      localAbstractCellElement = (AbstractCellElement)super.clone();
      localAbstractCellElement.setRow(paramInt2);
      localAbstractCellElement.setColumn(paramInt1);
      return localAbstractCellElement;
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      throw new RuntimeException(localCloneNotSupportedException);
    }
  }

  public CellElement deriveCellElement(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    AbstractCellElement localAbstractCellElement;
    try
    {
      localAbstractCellElement = (AbstractCellElement)super.clone();
      localAbstractCellElement.setRow(paramInt2);
      localAbstractCellElement.setRowSpan(paramInt4);
      localAbstractCellElement.setColumn(paramInt1);
      localAbstractCellElement.setColumnSpan(paramInt3);
      return localAbstractCellElement;
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      throw new RuntimeException(localCloneNotSupportedException);
    }
  }

  public CellElement clone(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
    throws CloneNotSupportedException
  {
    AbstractCellElement localAbstractCellElement = (AbstractCellElement)clone();
    localAbstractCellElement.setColumn(paramInt1);
    localAbstractCellElement.setRow(paramInt2);
    localAbstractCellElement.setColumnSpan(paramInt3);
    localAbstractCellElement.setRowSpan(paramInt4);
    return localAbstractCellElement;
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    AbstractCellElement localAbstractCellElement = (AbstractCellElement)super.clone();
    if ((getValue() != null) && (getValue() instanceof FCloneable))
      localAbstractCellElement.setValue(((FCloneable)getValue()).clone());
    localAbstractCellElement.optionalAttributes = ((EM)this.optionalAttributes.clone());
    if (getStyle() != null)
      localAbstractCellElement.setStyle(getStyle());
    if (this.attributes != null)
      localAbstractCellElement.attributes = BaseCoreUtils.cloneMap(this.attributes);
    return localAbstractCellElement;
  }

  private void readObject(ObjectInputStream paramObjectInputStream)
    throws ClassNotFoundException, IOException
  {
    paramObjectInputStream.defaultReadObject();
    Object localObject = paramObjectInputStream.readObject();
    if (localObject != null)
      if (localObject instanceof ImageSerializable)
        this.value = ((ImageSerializable)localObject).getImage();
      else
        this.value = localObject;
  }

  private void writeObject(ObjectOutputStream paramObjectOutputStream)
    throws IOException
  {
    paramObjectOutputStream.defaultWriteObject();
    if ((this.value != null) && (this.value instanceof Image))
    {
      ImageSerializable localImageSerializable = new ImageSerializable((Image)this.value);
      paramObjectOutputStream.writeObject(localImageSerializable);
    }
    else
    {
      paramObjectOutputStream.writeObject(this.value);
    }
  }
}